/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IFSv2.Forms.Utilities;

import IFSv2.Connection.DBConnection;
import IFSv2.Forms.Main.frmMain;
import IFSv2.Models.mdlCurrency;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Joel
 */
public class frmCurrency extends javax.swing.JDialog {

    mdlCurrencyFill currencyFill;
    String baseCurrency = "";
    
    public frmCurrency(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.setLocationRelativeTo(null);

        try {
            fillComponents();
            
        } catch (SQLException ex) {
            Logger.getLogger(frmCurrency.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
        loadComponents();
    }
    
    private void fillComponents() throws SQLException{
    
        ResultSet rsCurrencyCode;
        currencyFill = new mdlCurrencyFill();
        
        String _sql = "SELECT CurrencyCode, CurrencySymbol, CurrencyDescription, ExchangeRate, HomeCurrency FROM currencycode";
        
        rsCurrencyCode = DBConnection.getActiveConnection().createStatement().executeQuery(_sql);
        
        while(rsCurrencyCode.next()){
            currencyFill.addCurrency(rsCurrencyCode.getString("CurrencyCode"), rsCurrencyCode.getString("CurrencySymbol"), rsCurrencyCode.getString("CurrencyDescription"), rsCurrencyCode.getDouble("ExchangeRate"));
        }
        
        rsCurrencyCode.close();
        baseCurrency = mdlCurrency.currencyCode;
        
    }
    
    private void loadComponents(){
    
        for(int i=0 ; i<currencyFill.currencyCode.size(); i++){
            cbCurrency.addItem(currencyFill.getCurrencyCode(i));
        }
        cbCurrency.setSelectedItem(baseCurrency);
        txtBaseCurr.setText(baseCurrency);
    }
    
    private void updateCurrency(){
    
        if(this.cbCurrency.getSelectedIndex() != -1){
            final String currencyCode = this.cbCurrency.getSelectedItem().toString();
            txtSymbol.setText(currencyFill.getCurrencySymbol(currencyCode));
            txtDesc.setText(currencyFill.getCurrencyDescription(currencyCode));
            txtExchangeRate.setText(String.valueOf(currencyFill.getExchangeRate(currencyCode)));
            txtNewCurr.setText(currencyCode);
            txtNewCurr2.setText(currencyCode);
            txtCurr.setText(baseCurrency);
            txtCurr2.setText(baseCurrency);
            txtConversionMul.setText(String.valueOf(currencyFill.getExchangeRate(currencyCode)));
            Double amt = 1/currencyFill.getExchangeRate(currencyCode);
            amt = (double)(Math.round(amt*100))/100;
            txtConversionDiv.setText(String.valueOf(amt));
        
        }
    }
    
    private void loadCurrenctCurrency(){
        mdlCurrency.currencyCode = cbCurrency.getSelectedItem().toString();
        mdlCurrency.currencySymbol = txtSymbol.getText();
        mdlCurrency.exchangeRate = Double.parseDouble(txtExchangeRate.getText());
        frmMain.changeCurrency(cbCurrency.getSelectedItem().toString());
        this.dispose();
    }
    
    public class mdlCurrencyFill{
    
        private List<String> currencyCode;
        private List<String> currencySymbol;
        private List<String> currencyDescription;
        private List<Double> exchangeRate;
        
        
        public mdlCurrencyFill(){
        
            currencyCode = new ArrayList();
            currencySymbol = new ArrayList();
            currencyDescription = new ArrayList();
            exchangeRate = new ArrayList();
        
        }
        
        public void addCurrency(String currencyCode, String currencySymbol, String currencyDescription, Double exchangeRate){
            this.currencyCode.add(currencyCode); 
            this.currencySymbol.add(currencySymbol);
            this.currencyDescription.add(currencyDescription);
            this.exchangeRate.add(exchangeRate);
        }
        
        public String getCurrencyCode(int index){
            return this.currencyCode.get(index);
        }
        
        public String getCurrencySymbol(String currencyCode){
            return this.currencySymbol.get(this.currencyCode.indexOf(currencyCode));
        }
    
        public String getCurrencyDescription(String currencyCode){
            return this.currencyDescription.get(this.currencyCode.indexOf(currencyCode));
        }
        
        public Double getExchangeRate(String currencyCode){
            return this.exchangeRate.get(this.currencyCode.indexOf(currencyCode));
        }
    
    }
    

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbCurrency = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtSymbol = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDesc = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtBaseCurr = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtExchangeRate = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        cbMethod = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtNewCurr = new javax.swing.JTextField();
        txtCurr2 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtCurr = new javax.swing.JTextField();
        txtNewCurr2 = new javax.swing.JTextField();
        txtConversionMul = new javax.swing.JFormattedTextField();
        txtConversionDiv = new javax.swing.JFormattedTextField();
        btnOK = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Currency");

        cbCurrency.setSelectedItem(mdlCurrency.currencyCode);
        cbCurrency.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbCurrencyActionPerformed(evt);
            }
        });

        jLabel2.setText("Symbol");

        txtSymbol.setEditable(false);

        jLabel3.setText("Description");

        txtDesc.setEditable(false);

        jLabel4.setText("Base Currency");

        txtBaseCurr.setEnabled(false);

        jLabel5.setText("Exchange Rate");

        txtExchangeRate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));

        jLabel6.setText("Method");

        cbMethod.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SPOT", "EOM" }));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Currency Unit Equivalent"));

        jLabel7.setText("1.00");

        jLabel8.setText("1.00");

        txtNewCurr.setEditable(false);

        txtCurr2.setEditable(false);

        jLabel9.setText("=");

        jLabel10.setText("=");

        txtCurr.setEditable(false);

        txtNewCurr2.setEditable(false);

        txtConversionMul.setEditable(false);
        txtConversionMul.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00;(#,##0.00)"))));

        txtConversionDiv.setEditable(false);
        txtConversionDiv.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00;(#,##0.00)"))));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCurr2))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNewCurr, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtConversionMul, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                    .addComponent(txtConversionDiv))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtCurr)
                    .addComponent(txtNewCurr2, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtNewCurr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(txtCurr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtConversionMul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtCurr2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(txtNewCurr2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtConversionDiv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnOK.setText("OK");
        btnOK.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnOKMouseClicked(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cbCurrency, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtSymbol)
                            .addComponent(txtDesc)
                            .addComponent(cbMethod, 0, 110, Short.MAX_VALUE)
                            .addComponent(txtBaseCurr)
                            .addComponent(txtExchangeRate)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbCurrency, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtSymbol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtDesc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cbMethod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtBaseCurr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtExchangeRate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(btnOK))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbCurrencyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbCurrencyActionPerformed
        updateCurrency();
    }//GEN-LAST:event_cbCurrencyActionPerformed

    private void btnOKMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnOKMouseClicked
        if(evt.getButton() == MouseEvent.BUTTON1){
            loadCurrenctCurrency();
        }
    }//GEN-LAST:event_btnOKMouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        if(evt.getButton() == MouseEvent.BUTTON1)
            this.dispose();
    }//GEN-LAST:event_jButton2MouseClicked

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnOK;
    private javax.swing.JComboBox cbCurrency;
    private javax.swing.JComboBox cbMethod;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtBaseCurr;
    private javax.swing.JFormattedTextField txtConversionDiv;
    private javax.swing.JFormattedTextField txtConversionMul;
    private javax.swing.JTextField txtCurr;
    private javax.swing.JTextField txtCurr2;
    private javax.swing.JTextField txtDesc;
    private javax.swing.JFormattedTextField txtExchangeRate;
    private javax.swing.JTextField txtNewCurr;
    private javax.swing.JTextField txtNewCurr2;
    private javax.swing.JTextField txtSymbol;
    // End of variables declaration//GEN-END:variables
}
